package com.nt.oam.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.util.*;

import com.nt.common.core.domain.entity.SysRole;
import com.nt.common.core.domain.entity.SysUser;
import com.nt.common.enums.YesOrNoEnum;
import com.nt.common.utils.DateUtils;
import com.nt.common.utils.SecurityUtils;
import com.nt.common.utils.StringUtils;
import com.nt.oam.domain.*;
import com.nt.system.service.impl.SysRoleServiceImpl;
import com.nt.system.service.impl.SysUserServiceImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import com.nt.oam.mapper.AntHospitalMapper;
import com.nt.oam.service.IAntHospitalService;

/**
 * 医院信息Service业务层处理
 *
 * @author JZFeng
 * @date 2025-02-05
 */
@Service
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class AntHospitalServiceImpl extends ServiceImpl<AntHospitalMapper, AntHospital> implements IAntHospitalService {
    private final AntHospitalMapper antHospitalMapper;
    private final SysRoleServiceImpl sysRoleService;

    private final SysUserServiceImpl sysUserService;


    /**
     * 查询医院信息
     *
     * @param hospitalId 医院信息主键
     * @return 医院信息
     */
    @Override
    public AntHospital selectAntHospitalByHospitalId(Long hospitalId) {
        return antHospitalMapper.selectAntHospitalByHospitalId(hospitalId);
    }

    /**
     * 查询医院信息列表
     *
     * @param antHospital 医院信息
     * @return 医院信息
     */
    @Override
    public List<AntHospital> selectAntHospitalList(AntHospital antHospital) {
        return antHospitalMapper.selectAntHospitalList(antHospital);
    }

    /**
     * 新增医院信息
     *
     * @param antHospital 医院信息
     * @return 结果
     */
    @Override
    public int insertAntHospital(AntHospital antHospital) {
        antHospital.setCreateTime(DateUtils.getNowDate());
        String code = "0";
        // 查询,新增医院代码
        LambdaQueryWrapper<AntHospital> antHospitalLambdaQueryWrapper = new LambdaQueryWrapper<>();
        antHospitalLambdaQueryWrapper.orderByDesc(AntHospital::getHospitalId);
        antHospitalLambdaQueryWrapper.last("limit 1");
        AntHospital antHospitalCode = this.getOne(antHospitalLambdaQueryWrapper);
        if (Objects.nonNull(antHospitalCode)) {
            if(Objects.nonNull(antHospitalCode.getHospitalCode())){
                code = antHospitalCode.getHospitalCode();
            }
        }
        antHospital.setHospitalCode(String.valueOf(Integer.parseInt(code) + 1));
        // 创建人
        Long userId = SecurityUtils.getUserId();
        SysUser sysUser = sysUserService.selectUserById(userId);
        if (Objects.nonNull(sysUser) && StringUtils.isNotEmpty(sysUser.getUserName())) {
            antHospital.setCreatePerson(sysUser.getUserName());
        }
        // 是否删除，默认否
        antHospital.setIsDeleted(YesOrNoEnum.NO.getCode());
        antHospital.setIsModify(YesOrNoEnum.NO.getCode());
        return antHospitalMapper.insertAntHospital(antHospital);
    }

    /**
     * 修改医院信息
     *
     * @param antHospital 医院信息
     * @return 结果
     */
    @Override
    public int updateAntHospital(AntHospital antHospital) {
        antHospital.setUpdateTime(DateUtils.getNowDate());
        // 更新人
        Long userId = SecurityUtils.getUserId();
        SysUser sysUser = sysUserService.selectUserById(userId);
        if (Objects.nonNull(sysUser) && StringUtils.isNotEmpty(sysUser.getUserName())) {
            antHospital.setUpdatePerson(sysUser.getUserName());
        }
        return antHospitalMapper.updateAntHospital(antHospital);
    }
    /**
     * 批量删除医院信息
     *
     * @param hospitalIds 需要删除的医院信息主键
     * @return 结果
     */
    @Override
    public int deleteAntHospitalByHospitalIds(Long[] hospitalIds) {
        List<Long> hospitalIdsList = Arrays.asList(hospitalIds);
        hospitalIdsList.forEach(a -> {
            AntHospital antHospital = antHospitalMapper.selectAntHospitalByHospitalId(a);
            antHospital.setIsDeleted(YesOrNoEnum.YES.getCode());
            antHospitalMapper.updateAntHospital(antHospital);
        });
        // antHospitalMapper.deleteAntHospitalByHospitalIds(hospitalIds);
        return YesOrNoEnum.YES.getCode();
    }

    /**
     * 删除医院信息信息
     *
     * @param hospitalId 医院信息主键
     * @return 结果
     */
    @Override
    public int deleteAntHospitalByHospitalId(Long hospitalId) {
        return antHospitalMapper.deleteAntHospitalByHospitalId(hospitalId);
    }


    /**
     * 查询是否在修改
     */
    public int selectHospitalModify(Long hospitalId) {
        AntHospital antHospital = this.getById(hospitalId);
        int isModify = 0;
        if (Objects.nonNull(antHospital) && Objects.nonNull(antHospital.getIsModify())) {
            isModify = antHospital.getIsModify();
        }
        return isModify;
    }

    /**
     * 重新设置修改状态
     */
    public int updateHospitalModify(Long hospitalId, Integer isModify) {
        AntHospital antHospital = this.getById(hospitalId);
        if (Objects.nonNull(antHospital)) {
            antHospital.setIsModify(isModify);
        }
        return antHospitalMapper.updateAntHospital(antHospital);
    }
}
